
var merchantService = require('../../service/o2o_admin/merchant.server.service');
const winston = require('../../../app/utils/winstonLogger.js');

module.exports = {
    findAllMerchant : findAllMerchant,
    findMerchantByStatus :findMerchantByStatus,
    updateStatus :updateStatus,
    findMerchantById : findMerchantById
}

function findAllMerchant(req, res, next){
    winston.info('findAllMerchant controller...');
    merchantService.findAllMerchants((error, result)=>{
        if(error){
            winston.error(error.message);
            return next(new Error(error));
        }else{
            winston.info("find all merchant success!");
            res.json(result);
        }
    });
}

function findMerchantByStatus(req, res, next){
    winston.info('findMerchantByStatus controller...');
    let status = req.params.status;
    merchantService.findMerchantsByStatus(status, (error, result)=>{
        if(error){
            winston.error(error.message);
            return next(new Error(error));
        }else{
            winston.info("find merchant by status:"+ status +" success!");
            res.json(result);
        }
    });
}

function findMerchantById(req, res, next){
    winston.info('findMerchantById controller...');
    let merchantId = req.params.id;
    merchantService.findMerchantById(merchantId,(error, result)=>{
        if(error){
            winston.error(error.message);
            return next(new Error(error));
        }else{
            winston.info("find all by id:" + merchantId +" success!");
            res.json(result);
        }
    });
}

function updateStatus(req, res, next){
    winston.info('updateStatus controller...');
    let merchant = req.body;
    merchantService.updateMerchantStatus(merchant, (error, result)=>{
        if(error){
            winston.error(error.message);
            return next(new Error(error));
        }else{
            winston.info("updateStatus success!");
            res.json(result);
        }
    });
}

